<?php
/**
 * Created by PhpStorm.
 * User: lizx
 * Date: 2018/3/13
 * Time: 9:10
 */

namespace app\api\controller\v1;

use app\api\controller\Fun;
use app\api\service\ApplyShaohuoService;
use app\api\service\CommonFuncsService;
use app\common\Model\UserShaohuo;
use think\Config;

/**
 * @title 申请捎货人接口V1
 * @description 接口说明
 *
 * @header name:copyright require:1 default:v1 desc:版本号(全局)
 */
class Exam extends Auth
{
    /**
     * @title 提交申请信息
     * @description
     * @author lizx
     * @url /api/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Exam desc:接口组(全局)
     *
     * @param name:command type:string require:1 default: other:applyAction desc:用户信息
     * @param name:uid type:interval require:1 default: other: desc:用户id
     * @param name:token type:interval require:1 default: other: desc:用户token
     * @param name:realname type:interval require:1 default: other: desc:真实姓名
     * @param name:id_card type:interval require:1 default: other: desc:身份证号码
     * @param name:emergency_contact type:interval require:1 default: other: desc:紧急联系人姓名
     * @param name:emergency_contact_phone type:interval require:1 default: other: desc:紧急联系人手机号码
     * @param name:idcard_front type:interval require:1 default: other: desc:身份证正面照片
     * @param name:idcard_back type:interval require:1 default: other: desc:身份证背面照片
     * @param name:vehicle_license type:interval require:1 default: other: desc:行驶证正面照片
     * @param name:driver_license type:interval require:1 default: other: desc:驾驶证正面照片
     * @param name:sendway_id type:interval require:1 default: other: desc:交通工具0不限|1电动车|2汽车|3货车
     *
     * @return data:[]
     */
    public function applyAction()
    {
        //申请记录
        $info = UserShaohuo::get(['uid' => $this->arr['uid']]);

//        if(empty($info) || $info->exam_status == 0){
//            return Fun::error('801024');//请参加考试
//        }

        //验证姓名
        if(preg_match('/[\x{10000}-\x{10FFFF}]/u', $this->arr['realname'])
            || preg_match('/[\xf0-\xf7].{3}/', $this->arr['realname'])){
            return Fun::error('801007');
        }

        //验证手机号
        $mobile = CommonFuncsService::instance()->checkPhone($this->arr['emergency_contact_phone']);
        if($mobile == false) return Fun::error('801004');

        //验证身份证号码
        $idcard = CommonFuncsService::instance()->getIDCard($this->arr['id_card']);
        if($idcard == false)return Fun::error('801007');

        //是否18-60周岁之间
        if(!ApplyShaohuoService::instance()->isMeetAgeByIDCard($idcard, 18) ||
            ApplyShaohuoService::instance()->isMeetAgeByIDCard($idcard, 61)){
            return Fun::error('801025');
        }

        //组合数据
        $infoParam = array('realname' => $this->arr['realname'],
            'id_card' => $this->arr['id_card'],
            'emergency_contact' => $this->arr['emergency_contact'],
            'emergency_contact_phone' => $this->arr['emergency_contact_phone'],
        );

        //组合数据
        if(!empty($this->arr['idcard_front']))$infoParam['idcard_front'] = $this->arr['idcard_front'];
        if(!empty($this->arr['idcard_back']))$infoParam['idcard_back'] = $this->arr['idcard_back'];
        if(!empty($this->arr['vehicle_license']))$infoParam['vehicle_license'] = $this->arr['vehicle_license'];
        if(!empty($this->arr['driver_license']))$infoParam['driver_license'] = $this->arr['driver_license'];
        $infoParam['sendway_id'] = (isset($this->arr['sendway_id']) && !empty($this->arr['sendway_id'])) ? intval($this->arr['sendway_id']) : 0;

        //如果等于交通工具等于2，且原来没上传过
        if (empty($info['driver_license']) &&
            ($infoParam['sendway_id'] == 2 || $infoParam['sendway_id'] == 3) &&
            empty($this->arr['driver_license'])) {
            return Fun::error('801007');
        }

        $infoParam['apply_status'] = 1;//重新审核
        $rtn = UserShaohuo::where(['us_id' => $info['us_id']])->update($infoParam);

        if($rtn !== false){
            return Fun::success([],'申请成功');
        }else{
            return Fun::error('801026');
        }
    }


    /**
     * @title 查看提交信息
     * @description
     * @author lizx
     * @url /api/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Exam desc:接口组(全局)
     *
     * @param name:command type:string require:1 default: other:applyInfo desc:用户信息
     * @param name:uid type:interval require:1 default: other: desc:用户id
     * @param name:token type:interval require:1 default: other: desc:用户token
     * @param name:isfull type:interval require:1 default: other: desc:是否返回全部信息
     *
     * @return data:[]
     */
    public function applyInfo()
    {
        $isfull = $this->arr['isfull'];
        $uid = $this->arr['uid'];
        $info = UserShaohuo::get(['uid' => $uid]);

        if(empty($info)){
            return Fun::error('');//信息为空
        }

        $result = array(
            'time'   => time(),
            'us_id' => $info['us_id'],
            'status'   => $info['status'],
            'remark'   => $info['remark']
        );
        if($isfull){
            $result['realname'] = $info['realname'];
            $result['id_card'] = $info['id_card'];
            $result['emergency_contact'] = $info['emergency_contact'];
            $result['emergency_contact_phone'] = $info['emergency_contact_phone'];
            $result['idcard_front'] = $info['idcard_front'];
            $result['idcard_back'] = $info['idcard_back'];
            $result['vehicle_license'] = $info['vehicle_license'];
            $result['driver_license'] = $info['driver_license'];
            $result['create_time'] = $info['create_time'];
            $result['update_time'] = $info['update_time'];
            $result['sendway_id'] = $info['sendway_id'];
        }
        return Fun::success($result,'查询成功');
    }
}